<template>
    <div class="tab-container">
      <h1>加班管理</h1>
      <el-button type="primary" @click="dialogFormVisible = true">添加加班记录</el-button>
      <el-dialog title="申请加班" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="员工" :label-width="'120px'">
                <el-select v-model="form.empNo" placeholder="请选择加班员工">
                  <el-option v-for="(v,i) in empList" :key="i" :label="v.empName" :value="v.empNo"></el-option>
                </el-select>
          </el-form-item>

          <el-form-item label="加班开始时间" :label-width="'120px'">
            <el-time-picker
              v-model="form.startTime"
              :picker-options="{
              }"
              placeholder="选择开始时间">
            </el-time-picker>
          </el-form-item>
          <el-form-item label="加班结束时间" :label-width="'120px'">
            <el-time-picker
              v-model="form.endTime"
              :picker-options="{
              }"
              placeholder="选择结束时间">
            </el-time-picker>
          </el-form-item>
          <el-form-item label="加班时长" :label-width="'120px'">
            <el-input v-model="getHours" autocomplete="off"></el-input>
          </el-form-item>
     
          <el-form-item label="加班原因" :label-width="'120px'">
            <el-input v-model="form.reason" width="100px" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="审批人" :label-width="'120px'">
            <el-select v-model="form.empNo" placeholder="请选择审批人">
              <el-option v-for="(v, i) in empList2" :key="i" :label="v.empName" :value="v.empNo"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false;form={}">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>
      </el-dialog>
      <el-table
      :data="list"
      style="width: 100%">
      <el-table-column
        prop="empName"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="startTime"
        label="加班开始时间"
        width="180">
      </el-table-column>
      <el-table-column
        prop="endDate"
        label="加班结束时间">
      </el-table-column>
      <el-table-column
        prop="hours"
        label="加班时长">
      </el-table-column>
      <el-table-column
        prop="reason"
        label="加班原因">
      </el-table-column>
      <el-table-column
        prop="auditorName"
        label="审批人">
      </el-table-column>
      <el-table-column
        prop="auditTime"
        label="审批时间">
      </el-table-column>
    </el-table>
    </div>
  </template>
  
  <script>
import { getOvertimeList,saveOvertime,getUser } from '@/api/overtime';
import {getEmpList,getEmpList2} from '@/api/emp'
  
  export default {
    name: 'Overtime',
    data() {
      return {
        list:[],
        dialogFormVisible:false,
        form:{},
        empList:[],
        empList2:[]
      }
    },
    created(){
      getEmpList().then(res => {
          this.empList = res.rows
        })

        getEmpList2().then(res => {
          this.empList2 = res.rows
        })
        this.getList();
    },
    methods:{
     async getList(){
       let res = await  getOvertimeList();

        for(let i=0;i<res.rows.length;i++){
          let reslut =  await getUser(res.rows[i].empNo);
          res.rows[i].empName = reslut.data.empName;
        }
        this.list = res.rows
        console.log("123321",this.list)
      },
      formatTime(timeObj){
        let y = timeObj.getFullYear();
        let m = timeObj.getMonth()+1;
        let d = timeObj.getDate();
        let h = timeObj.getHours();
        return `${y}-${m}-${d} ${h}`;
      },
      save(){
        this.form.hours = this.getHours;
        // console.log(this.form.startTime.get);
        this.form.startTime = this.formatTime(this.form.startTime);
        this.form.endTime = this.formatTime(this.form.endTime);
        
        console.log("添加的数据为：",this.form);
        saveOvertime(this.form).then(res=>{
          if(res.code == 200){
            this.$message('保存成功');
            this.dialogFormVisible = false;
            this.getList();
          }
        })
      },
      update(i,v){
        this.form = v;
        this.dialogFormVisible = true;
      }
    },
    computed:{
      getHours(){
        if(!this.form.startTime || !this.form.endTime) return 0;
   

        let diff = this.form.endTime - this.form.startTime;

        let hour = Math.floor(diff / 1000 / 60 /60);

        return hour;
      }
    }
  }
  </script>
  
  <style scoped>
   
  </style>
  